package com.taxsystem.chenliangli.askquestion_tool;

import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.taxsystem.chenliangli.askquestion.data.answer;



import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;



/*
 * 将// 注册广播的方式定义到这个类中   
 * 向服务器发送请求，并获得返回数据
 * ////将获得的回答结果整理  放到相应的answer数组中
			//h获得的数据的结构是
			/*
			  {
					"data":[{"questionId": "1", "questionTitle": "开发票需要什么资料", "createTime": "2014-04-29 09:36:55","answerCount": "1"
				},{
					"questionId": "2", "questionTitle": "个人所得税缴税流程是什么", "createTime": "2014-04-29 09:30:55","answerCount": "1"}]
				}
*/

public class QuestionAnswer extends HttpUtil {
	public final static String RECI_COAST = "sw.system.HTTUTIL";
	private static String URL = "3q/app/question/listall?";
	private JSONObject jsonanswer=new JSONObject();
	
	////根据style的值来判断：1:查看所有问题；   2：查询答复问题； 3：录入问题
	//并返回搜索到的结果
	public  answer[]  getAnswer(int  style,String fuzzyquuery)
	{
		
		switch(style)
		{
		case 1:{URL=URL+"public=-1"+"&hasanswer=-1"+"fuzzyquuery="+fuzzyquuery;}break;
		case 2:{URL=URL+"public=-1"+"&hasanswer=1"+"fuzzyquuery="+fuzzyquuery;}break;
		case 3:{URL=URL+"public=-1"+"&hasanswer=-1"+"fuzzyquuery="+fuzzyquuery;}break;
		}
		
		
		try {
			
			
			//注册广播并向服务器端发送请求，将返回的数据放到jsonanseer中       有点问题  想不要
		      //GetMyBoradcast();
			
		      JSONArray jsonArray;
			  jsonArray = jsonanswer.getJSONArray("data");
			  answer[] ans=new answer[jsonArray.length()];
				for (int i = 0; i < jsonArray.length(); i++) {
					    System.out.println("element " + i + " :" + jsonArray.get(i));
					    JSONObject j1=(JSONObject)jsonArray.get(i);
					    ans[i].setId(j1.getString("questionId"));
					    ans[i].setTitle(j1.getString("questionTitle"));
					    ans[i].setCreateTime(j1.getString("createTime"));
					    ans[i].setAnswerCount(j1.getString("answerCount"));   
			   }
				
				return ans;
			
			} catch (JSONException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			
			
			return null;
	}
	
	
	
	
	
	//注册广播
	public void GetMyBoradcast()
	{
		IntentFilter filter = new IntentFilter(RECI_COAST);
		BroadcastReceiver myrecive = new MyRecive();
		
		//registerReceiver(myrecive, filter);
		Thread thread = new Thread() 
		{

			@Override
			public void run() 
			{
				Intent intent = new Intent(RECI_COAST);
				try {
					//获取服务器返回的信息
					String reslut = HttpUtil
							.getRequest(URL);
					intent.putExtra("answer", reslut);
					//发送广播
					//sendBroadcast(intent);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}

		};
		
	}

	// 定义BroadcastReceiver 接收返回的信息
	private class MyRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				//获取JSONObject对象
				JSONObject jsonobject = new JSONObject(
						intent.getStringExtra("answer"));
				 jsonanswer = new JSONObject(
						jsonobject.getString("answer"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}


	
	
	
	

	public JSONObject getJsonanswer() {
		return jsonanswer;
	}


	public void setJsonanswer(JSONObject jsonanswer) {
		this.jsonanswer = jsonanswer;
	}


	
	
	
}
